#!/usr/bin/env python
#-*- coding: utf-8 -*-

# @autor: Elísio Gonçalves Gomes Ribeiro
# @curso: Engenharia Informática 3ºano nº4708
# @subject: Estrutura de Dados e Algoritmo
# @@version: 1.0.0 2012/2013
# @google-code:

###############################################################################
#
#
#

#------------------------------------------------------------------------------

import matplotlib.pyplot as plt
import numpy as np
from math import *



def calc_avrg(lst):
    return float(sum(lst)) / len(lst)

def calc_avg_dev(lst):
    dev = 0.0
    avrge = 0.0
    avrge = calc_avrg(lst)
    for val in lst:
        dev += (pow(val,2) - pow(avrge,2))
    pass
    dev = dev / len(lst)
    return round(dev,6), round(avrge,6)

def draw_graph(xx, yy, ax,width):	
    tval = []
    xv=np.arange(xx-(width*2),xx+(width*3), width)

    xtick = ax.bar(xv[0], yy[0], width=width, color='g')
    tval.append(xtick[0])
    
    xtick = ax.bar(xv[1], yy[1], width=width, color='b')
    tval.append(xtick[0])
    
    xtick = ax.bar(xv[2], yy[2], width=width, color='r')
    tval.append(xtick[0])
    
    xtick = ax.bar(xv[3], yy[3], width=width, color='c')
    tval.append(xtick[0])
    
    xtick = ax.bar(xv[4], yy[4], width=width, color='y')
    tval.append(xtick[0])
    
    return tval

def draw_g_algo(algo, lsts, el_time, mVal, dVal):
    fig = plt.figure()
    ax1 = fig.add_subplot(111)
    ax1.plot(lsts, el_time, 'ro', lsts, mVal, 'bo', lsts, dVal, 'go')
    
    ax1.set_ylabel('Time / Mean / Dev')
    ax1.set_title(algo+' Algoritms Values')
    ax1.grid()
    ax1.legend( ('el.Times', 'Mean', 'Deviation'), loc='best')
    fn = 'test/'+algo+'_graph.png'
    fig.savefig(fn,DPI=500)
    return fn

def draw44(xx, yy, labellst):
    fig = plt.figure()
    ax = fig.add_subplot(111)
    width = (1.-5.*0.05)/len(labellst)
    xtval = []
    xd=np.arange(len(xx))

    for k in range(len(xx)):
        xv = xd[k]+(k*width)
        xtval = draw_graph(xv, yy[k], ax, width)
    pass

    ax.set_ylabel('Mean Values')
    ax.set_title('Mean of Measured Times for Ordering Algoritms')
    ax.set_xticks(xd+width)
    ax.set_xticklabels( (xx ))
    ax.grid()
    ax.legend( xtval, labellst, loc='best')
    fn = 'test/average_graph.png'
    fig.savefig(fn,DPI=500)
    return fn
